package com.ddtech.business.bank.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ddtech.business.bank.entity.QuestionBankQuestionOption;
import com.ddtech.business.bank.mapper.QuestionBankQuestionOptionMapper;
import com.ddtech.business.common.BaseService;
import com.ddtech.business.constant.BusinessEnum;
import com.ddtech.exception.BizPreconditions;
import com.ddtech.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 题目选项服务类
 * @author  bin
 * @date  2023/2/11 15:32
 */
@Service
public class QuestionBankQuestionOptionService extends BaseService<QuestionBankQuestionOptionMapper, QuestionBankQuestionOption> {

    public void doSaveOrUpdate(QuestionBankQuestionOption questionBankQuestionOption) {
        questionBankQuestionOption.init();
        this.saveOrUpdate(questionBankQuestionOption);
    }

    public void delete(String id) {
        QuestionBankQuestionOption questionBankQuestionOption = this.getById(id);
        BizPreconditions.check(questionBankQuestionOption == null, BusinessEnum.OPERATE_ERROR.getMsg());
        questionBankQuestionOption.baseDelete();
        this.saveOrUpdate(questionBankQuestionOption);
    }

    /**
     * 根据examId 和 questionId 查询QuestionBankQuestionOption列表
     *
     * @param bankId
     * @param questionId
     * @return
     */
    public List<QuestionBankQuestionOption> queryoptList(String bankId, String questionId) {
        QueryWrapper<QuestionBankQuestionOption> queryWrapper = this.newValidQueryWrapper();
        if (StringUtils.isNotBlank(bankId)) {
            queryWrapper.eq(QuestionBankQuestionOption.BANK_ID, bankId);
        }
        if (StringUtils.isNotBlank(questionId)) {
            queryWrapper.eq(QuestionBankQuestionOption.QUESTION_ID, questionId);
        }
        return this.list(queryWrapper);
    }

    /**
     * 根据题库id批量删除题目选项
     *
     * @param bankId
     */
    public void batchDeleteQuestionBankQuestionOption(String bankId) {
        QueryWrapper<QuestionBankQuestionOption> queryWrapper = this.newValidQueryWrapper();
        queryWrapper.eq(QuestionBankQuestionOption.BANK_ID, bankId);
        List<QuestionBankQuestionOption> list = this.list(queryWrapper);
        for (QuestionBankQuestionOption questionBankQuestionOption : list) {
            questionBankQuestionOption.baseDelete();
            this.saveOrUpdate(questionBankQuestionOption);
        }
    }
    /***
     * description: 根据题目ID删除选项
     * @param:  * @param questionId
     * @date: 2023-04-28
     * @return: void
     * @version v1.0
     * @author:wei_zhang(xiao)
     */
    public void batchDeleteQuestionBankQuestionOptionByQuestionId(String questionId) {
        QueryWrapper<QuestionBankQuestionOption> queryWrapper = this.newValidQueryWrapper();
        queryWrapper.eq(QuestionBankQuestionOption.QUESTION_ID, questionId);
        List<QuestionBankQuestionOption> list = this.list(queryWrapper);
        for (QuestionBankQuestionOption questionBankQuestionOption : list) {
//            questionBankQuestionOption.baseDelete();
//            this.saveOrUpdate(questionBankQuestionOption);
            this.removeById(questionBankQuestionOption.getId());
        }
    }


}
